<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-input v-model="query.UserName" placeholder="账号名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" @click="Add">添加</el-button>
                <el-button type="primary" @click="Addrole">添加角色</el-button>

                <!-- 角色添加弹出框 -->
                <el-dialog title="角色添加" :visible.sync="editVisiblett" width="30%">

                    <el-table
                        :data="tableDatat"
                        border
                        class="table"
                        ref="multipleTable"
                        header-cell-class-name="table-header"
                        @selection-change="handleSelectionChange"
                    >
                        <el-table-column type="selection" align="center"></el-table-column>
                        <el-table-column prop="rId" label="ID" align="center"></el-table-column>
                        <el-table-column prop="roleName" label="角色名称"></el-table-column>
                    </el-table>

                    <span slot="footer" class="dialog-footer">
                        <el-button @click="editVisiblett = false">取 消</el-button>
                        <el-button type="primary" @click="Addt">确 定</el-button>
                    </span>
                </el-dialog>

                <!-- 添加弹出框 -->
                <el-dialog title="添加" :visible.sync="editVisiblet" width="30%">
                    <el-form ref="formDate" :model="formDate" label-width="70px">
                        <el-form-item label="账号名称">
                            <el-input v-model="formDate.userName"></el-input>
                        </el-form-item>
                        <el-form-item label="密码">
                            <el-input v-model="formDate.userPassword"></el-input>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="editVisiblet = false">取 消</el-button>
                        <el-button type="primary" @click="Addt">确 定</el-button>
                    </span>
                </el-dialog>
            </div>
            <el-row>
                <el-col :span="4">
                    <div class="grid-content bg-purple">
                        <el-tree :data="data" :props="defaultProps" accordion @node-click="handleNodeClick"> </el-tree>
                    </div>
                </el-col>
                <el-col :span="20">
                    <div class="grid-content bg-purple-light">
                        <el-table
                            :data="tableData"
                            border
                            class="table"
                            ref="multipleTable"
                            header-cell-class-name="table-header"
                            @selection-change="handleSelectionChange"
                        >
                            <el-table-column type="selection" align="center"></el-table-column>
                            <el-table-column prop="uId" label="ID" align="center"></el-table-column>
                            <el-table-column prop="userName" label="账号名称"></el-table-column>
                            <el-table-column prop="userPassword" label="密码"></el-table-column>
                            <el-table-column prop="roleName" label="角色"></el-table-column>
                            <el-table-column label="操作" align="center">
                                <template slot-scope="scope">
                                    <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                                    <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                                        >删除</el-button
                                    >
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </el-col>
            </el-row>

            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.Pageindex"
                    :page-size="query.Pagesize"
                    :total="Total"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="账号名称">
                    <el-input v-model="form.userName"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.userPassword"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
//导入api文件夹下的index.js文件中的fethData方法对象
import { getUserList, UptUser, DelUserList, AddUser,getRoleList } from '@/api/user';

export default {
    name: 'basetable',
    data() {
        return {
            data: [
                {
                    label: '上海',
                    children: [
                        {
                            label: '奉贤',
                            children: [
                                {
                                    label: '小庙村'
                                }
                            ]
                        }
                    ]
                },
                {
                    label: '北京',
                    children: [
                        {
                            label: '浙海',
                            children: [
                                {
                                    label: '俞飞村'
                                }
                            ]
                        },
                        {
                            label: '恢复',
                            children: [
                                {
                                    label: '黄飞村'
                                }
                            ]
                        }
                    ]
                },
                {
                    label: '天津',
                    children: [
                        {
                            label: '玫瑰',
                            children: [
                                {
                                    label: '授否村'
                                }
                            ]
                        },
                        {
                            label: '浦东',
                            children: [
                                {
                                    label: '微风村'
                                }
                            ]
                        }
                    ]
                }
            ],
            query: {
                UserName: '',
                Pageindex: 1,
                Pagesize: 10
            },
            tableData: [],
            tableDatat: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,

            editVisiblet: false,

            editVisiblett: false,

            Total: 0,
            form: {
                userName: '',
                userPassword: ''
            },
            formDate: {
                userName: '',
                userPassword: ''
            },
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        Addrole() {
            this.editVisiblett = true;
            this.getDatat();
        },
        getDatat(){
            getRoleList(this.query).then((res) => {
                //这里的res即使res.data对象,拦截器返回了.data
                console.log(res);
                console.log(res.plist);
                console.log(res.total);

                this.tableDatat = res.plist;
                this.Total = res.total || 50;
            });
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            getUserList(this.query).then((res) => {
                //这里的res即使res.data对象,拦截器返回了.data
                console.log(res);
                console.log(res.plist);
                console.log(res.total);

                this.tableData = res.plist;
                this.Total = res.total || 50;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'Pageindex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            console.log(index, row);
            console.log('删除返回的ID是：', row.uId);
            if (confirm('确认删除吗？')) {
                DelUserList({ Id: row.uId }).then((res) => {
                    if (res > 0) {
                        alert('删除成功！');
                        this.getData();
                    } else {
                        alert('删除失败！');
                    }
                });
            }
            // 二次确认删除
            // this.$confirm('确定要删除吗？', '提示', {
            //     type: 'warning'
            // })
            //     .then(() => {
            //         this.$message.success('删除成功');
            //         this.tableData.splice(index, 1);
            //     })
            //     .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        //添加操作
        Add() {
            this.editVisiblet = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            UptUser(this.form).then((res) => {
                console.log(res);
                this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                this.getData();
            });
            // this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            // this.$set(this.tableData, this.idx, this.form);
        },
        Addt() {
            this.editVisiblet = false;
            AddUser(this.formDate).then((res) => {
                console.log(res);
                this.$message.success('添加成功！');
                this.getData();
            });
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'Pageindex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
